﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using TEAA;

namespace TEAA
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        private SystemManager sm;

        protected void Page_Load(object sender, EventArgs e)
        {
            Session["page"] = "Select";
            sm = SystemManager.GetSystemManager();
        }

        protected void ButtonExecuteSelect_Click(object sender, EventArgs e)
        {
            //try
            //{
                string queryText = QueryTextBox.Text;
                if (queryText == null || queryText.Equals(string.Empty))
                    throw new Exception("No query given to process!");
                IDictionary<CommandType, List<string>> parsedQuery = DBParser.ParseQuery(queryText);
                List<RelationalOperation> ops = new List<RelationalOperation>();
                string table = Translator.TranslateSql(parsedQuery, ops);
                RecordManager rm = BinaryRecordManager.GetRecordManager();
                System.Diagnostics.Trace.WriteLine("tab " + rm.getAll(table).Tables[0].Columns[1].ColumnName);
                DataSet ds = ops[0].Execute(rm.getAll(table));
                System.Diagnostics.Trace.WriteLine("rows " + ds.Tables[0].Rows.Count);
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            //}
            //catch (Exception ex)
            //{
            //    alertBox(ex);
            //}
        }

        private void alertBox(Exception ex)
        {
            Response.Write("<script type=\"text/javascript\" language=\"javascript\">");
            string errorMessage = "";
            Exception exception = ex;
            while (exception != null)
            {
                errorMessage += exception.Message + "\\n";
                exception = exception.InnerException;
            }
            Response.Write("alert('" + errorMessage + "');");
            Response.Write("</script>");
        }

    }
}
